/**
 * 药品库
 */
Lib = cc.Sprite.extend({
	libArr: [],
	openFlag:false,
	doing:false,
	rm : 5,
	ctor:function(p){
		this._super("#lib_back.png");
		p.addChild(this,30);
		this.setTag(TAG_LIB);
		this.init();
	},
	init:function(){
		this.libArr = [];
		this.openFlag = false;
		this.setPosition(gg.width + this.width * 0.5,550);
		
		var book=new LibButton(this,10,TAG_LIB_BOOK,"#book1.png",this.callback,this);
		book.setPosition(cc.p(60,this.height*0.5));
		
		var weighing=new LibButton(this,10,TAG_LIB_WEIGHING,"#WeighingBottle/c2.png",this.callback,this);
		weighing.right(book,this.rm);
		
		var brush=new LibButton(this,10,TAG_LIB_BRUSH,"#brush.png",this.callback,this);
		brush.right(weighing, this.rm);
		
		var beaker=new LibButton(this,10,TAG_LIB_BEAKER,"#beaker.png",this.callback,this);
		beaker.right(brush, this.rm);
		
		var kettle=new LibButton(this,10,TAG_LIB_KETTLE,"#kettle.png",this.callback,this);
		kettle.right(beaker,this.rm);
		
		var glassRod=new LibButton(this,10,TAG_LIB_GLASS,"#Glass rod/right2.png",this.callback,this);
		glassRod.right(kettle,this.rm);
		
		var flask=new LibButton(this,10,TAG_LIB_FLASK,"#flask/flask1.png",this.callback,this);
		flask.right(glassRod, this.rm);
		
		var drop=new LibButton(this,10,TAG_LIB_DROP,"#drop.png",this.callback,this);
		drop.right(flask,this.rm);
	},
	moveLib:function(tag,width){
		width = 75;
		var begin = false;
		for(var i in this.libArr){
			var libTag = this.libArr[i];
			if(tag == libTag){
				begin = true;
			}
			if(begin){
				var lib = this.getChildByTag(libTag);
				if(lib != null){
					lib.runAction(cc.moveBy(0.5,cc.p(-width, 0)));
				}
			}
		}
	},
	callback:function(p){
		var pos = this.getPosition(); 
		var action = gg.flow.flow.action;
		switch(p.getTag()){
		case TAG_LIB_WEIGHING:
			ll.run.loadWeighing(pos);
			break;
		case TAG_LIB_BOOK:
			ll.run.loadBook(pos);
			break;
		case TAG_LIB_BEAKER:
			ll.run.loadBeaker(pos);
			break;
		case TAG_LIB_BRUSH:
			ll.run.loadBrush(pos);
			break;
		case TAG_LIB_KETTLE:
			ll.run.loadKettle(pos);
			break;
		case TAG_LIB_GLASS:
			ll.run.loadGlass(pos);
			break;
		case TAG_LIB_FLASK:
			ll.run.loadFlask(pos);
			break;
		case TAG_LIB_DROP:
			ll.run.loadDrop(pos);
			break;
		default:
			break;
		}
		if(action == ACTION_NONE){
			this.moveLib(p.getTag(), p.width * p.getScale());
			p.removeFromParent(true);
		}
	},
	isOpen:function(){
		return this.openFlag; 
	},
	open:function(){
		if(this.openFlag || this.doing){
			return;
		}
		this.doing = true;
		var move = cc.moveBy(0.4, cc.p(-this.width,0));
		var func = cc.callFunc(function(){
			this.openFlag = true;
			this.doing = false;
			var tag = gg.flow.flow.tag;
			 if(tag instanceof Array){
				 if(TAG_LIB_MIN < tag[1]){
					 // 显示箭头
					 gg.flow.location();
				 }
			 }
		}, this);
		var seq = cc.sequence(move,func);
		this.runAction(seq);
	},
	close:function(){
		if(!this.openFlag || this.doing){
			return;
		}
		this.doing = true;
		var move = cc.moveBy(0.4, cc.p(this.width,0));
		var func = cc.callFunc(function(){
			this.openFlag = false;
			this.doing = false;
			var tag = gg.flow.flow.tag;
			if(tag instanceof Array){
				if(TAG_LIB_MIN < tag[1]){
					// 隐藏箭头
//					ll.tip.arr.out();
					ll.tip.arr.pos(ll.tool.getChildByTag(TAG_BUTTON_LIB));	
				}
			}
		}, this);
		var seq = cc.sequence(move,func);
		this.runAction(seq);
	}
});
TAG_LIB_MIN = 30000;
TAG_LIB_WEIGHING=30001;
TAG_LIB_BOOK=30002;
TAG_LIB_BEAKER=30003;
TAG_LIB_BRUSH=30004;
TAG_LIB_KETTLE=30005;
TAG_LIB_GLASS=30006;
TAG_LIB_FLASK=30007;
TAG_LIB_DROP=30008;
libRelArr = [
             {tag:TAG_LIB_MIN, name:""},
             {tag:TAG_LIB_WEIGHING, name:"称量瓶"},
             {tag:TAG_LIB_BOOK,name:"记录本"},
             {tag:TAG_LIB_BEAKER,name:"烧杯"},
             {tag:TAG_LIB_BRUSH,name:"刷子"},
             {tag:TAG_LIB_KETTLE,name:"蒸馏水瓶"},
             {tag:TAG_LIB_GLASS,name:"玻璃棒"},
             {tag:TAG_LIB_FLASK,name:"烧瓶"},
             {tag:TAG_LIB_DROP,name:"胶头滴管"}
             ];
